<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends MX_Controller {
 	function __construct()
 	{
    	parent::__construct();
    	$this->load->library('Admin_Layout');
	 	$this->load->model('user/admin_user_model');
	 }
	 
 	function index($page='')
 	{ 		
 		$this->mycommon->authAdmin();
 		$permission = $this->session->userdata('permission');
 		if($permission != 1)
 			redirect(site_url('admin'));
        $this->admin_layout->_loadHeader("");
        $this->admin_layout->_loadLeftMenuUser();
		$data = array();
		$data['errors'] = '';			 		
       	$tmp = $this->admin_user_model->get_count_user();       	
       	$page = ($page != '') ? intval($page) : 1;
        $this->load->library('pagination');         
        $config['base_url'] = site_url('admin/user/index');
        $config['total_rows'] = $tmp[0]['user_count'];
        $config['per_page'] = 10;
        $config['use_page_numbers'] = TRUE;        
        $config['cur_page'] = $page;
        $config['uri_segment'] = 4;        
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        $data['cur_page'] = $page;
        $data['total_page'] = $this->pagination->num_links;  
        $data['infos'] = $this->admin_user_model->get_all_user($start, $config['per_page']);        
        if($this->input->post())
		{			
			$option = $this->input->post('applyfield');					
			if(!$option) $option = 0;
			$check_items = $this->input->post('check_item');				
			if($check_items){
				foreach($check_items as $check_item)
				{
					if($option == 1){						
						$result = $this->delete($check_item);
						if(!$result)
							$data['error'] = 1;
					}
					if($option == 2){						
						$user = array(	'id'=>$check_item,
										 	'status'=>2									
											);
						$result = $this->admin_user_model->update_user($user);
						if(!$result)
							$data['error'] = 1;																
					}
				}
				if($result)
					redirect(site_url('admin/user/index'));
			}				
		}	        
		
   	 	$this->load->view('user/user_view', $data);
   	 	$this->admin_layout->_loadFooter("");
 	}

	public function delete($id)
    {
    	$this->mycommon->authAdmin();
    	$result = $this->admin_user_model->delete_user($id);
    	if($result)
    		redirect(site_url('admin/user'));	    	
    }
    
    public function update()
    {    	
    	$this->mycommon->authAdmin();
    	$data = array();
    	$data['error'] = "";
    	$user = $this->input->post();
    	if($user['status'] != 0 && $user['status'] != 2)
    		$user['status'] = 1;
    	if($user['status'] == 0)
    		$result = $this->delete($user['id']);
    	else    	
    		$result = $this->admin_user_model->update_user($user);
    	if(!$result){
    		 $data['error'] = 1;    		 
    	}    	    		        	
    	echo json_encode($data);
    }
 	
 	public function changepassword()
 	{
 		$this->mycommon->authAdmin();
 		$permission = $this->session->userdata('permission');
 		if($permission != 1 && $permission != 2)
 			redirect(site_url('admin'));
 		$data = array();
 		$data['error'] = '';
 		$data['success'] = '';
        $this->minifier->AddJsNoPack('jquery.validate');        
        $this->admin_layout->_loadHeader("");
        $this->admin_layout->_loadLeftMenuUser();
        $user_id = $this->session->userdata('user_id');        
		if($this->input->post())
		{
			$oldpass = $this->input->post('oldpass');
			$newpass = $this->input->post('newpass');
			$renewpass = $this->input->post('renewpass');
			$user = $this->admin_user_model->get_user_by_id($user_id);
			if($user['password'] != md5($oldpass)){
				$data['error'] = 'Nhập sai password!';
			}
			else{
				if(strlen($newpass) < 6){
					$data['error'] = 'Password không đủ kí tự!';
				}
				else{					
					if($newpass != $renewpass){
						$data['error'] = 'Password mới không giống nhau!';
					}
					else{
						$user_data = array('id'=>$user_id,
											'password'=>md5($newpass)
											);
						$result = $this->admin_user_model->update_user($user_data);
						if(!$result)
							$data['error'] = "Thay đổi password không thành công!";
						else 
							$data['success'] = "Thay đổi password thành công!";
					}
				}				
			}			
		}							
	 	$data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken',$data['frmToken']);
   	 	$this->load->view('user/changepass_view', $data);
   	 	$this->admin_layout->_loadFooter("");
 	}
 	
 	public function setuser()
 	{
 		$this->mycommon->authAdmin(); 		
 		$permission = $this->session->userdata('permission'); 		
 		if($permission != 1)
 			redirect(site_url('admin'));
 		$data = array();
 		$data['error'] = '';
 		$data['success'] = '';
        $this->minifier->AddJsNoPack('jquery.validate');        
        $this->admin_layout->_loadHeader("");
        $this->admin_layout->_loadLeftMenuUser();               
		if($this->input->post())
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$user = $this->admin_user_model->get_user_by_username($username);
			if($user){
				$data['error'] = "Tên user đã tồn tại!";				
			}			
			else{
				if(strlen($username) < 3)
					$data['error'] = "Username phải lớn hơn 3 kí tự!";
				else{
					if(strlen($password) < 6)
						$data['error'] = "Password phải lớn hơn 6 kí tự!";
					else{
						$user_data = array('username'=>$username,
				    					'password'=>md5($password),
				    					'permission'=>2,
				    					'status'=>1
				    					);
						$result = $this->admin_user_model->set_user($user_data);
						if($result)
							$data['success'] = "Tạo mới thành công";
						else 	
							$data['error'] = "Tạo mới không thành công";	
					}	
				}
			}
		}							
	 	$data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken',$data['frmToken']);
   	 	$this->load->view('user/setuser_view', $data);
   	 	$this->admin_layout->_loadFooter("");
 	}
}
?>
